<template>
  <a-modal
    :title="title"
    :width="800"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="表单编码">
          <a-input placeholder="请输入表单编码" v-decorator="['formCode', validatorRules.formCode]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="表单名称">
          <a-input placeholder="请输入表单名称" v-decorator="['formName', validatorRules.formName]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工作流程">
          <a-select placeholder="请选择" v-model="model.flagKey" style="width: 100%">
            <a-select-option v-for="value in processDefinitionList" :key="value.key">
              {{ value.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="支出事项中的显示名称">
          <a-input placeholder="请输入支出事项中的显示名称" v-decorator="['planReimb', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
          <span slot="label">表单描述
            <a-popover>
              <template slot="content">
                <p>填写格式</p>
                <p>附件标题：必填/不必填</p>
                <p>图片标题：必填/不必填</p>
              </template>
              <a-icon type="question-circle" style="color: #ff7e24"/>
            </a-popover>
          </span>
          <a-textarea maxLength="1000" placeholder="请输入表单描述" v-decorator="['formDescribe', validatorRules.formDescribe]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import { duplicateCheck } from '@/api/api'

export default {
  name: 'TpFormInfoModal',
  data() {
    return {
      processDefinitionList: [], //工作流模板
      title: '操作',
      visible: false,
      model: { planReimb: 'zc' },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        formCode: { rules: [{ required: true, message: '请输入表单编码！' }] },
        formName: { rules: [{ required: true, message: '请输入表单名称！' }] },
        formDescribe:{initialValue:'附件：必填' +'\n'+
                  '图片：必填'},
      },
      url: {
        add: '/form/tpFormInfo/add',
        edit: '/form/tpFormInfo/edit',
        checkIsRepeat: '/form/tpFormInfo/checkIsRepeat',
      },
      isCopy: false//是否是复制的标识
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({planReimb:'zc'})
    },
    edit(record) {
      getAction('/act/procdef/queryLastVersionList').then((res) => {
        this.processDefinitionList = res
      })
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'formName', 'formDescribe', 'formCode','planReimb'))
        //时间格式化
      })
    },
    //复制
    copy(record){
      this.edit(record)
      this.model.copyFormCode = this.model.formCode
      this.isCopy = true
    },
    close() {
      this.$emit('close')
      this.isCopy = false
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id && !this.isCopy) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          //表单编码和申请或支出同时验证
          if(this.isCopy){
            this.model.id = null
          }
          getAction(this.url.checkIsRepeat, {
            id: this.model.id,
            formCode: this.form.getFieldValue('formCode'),
            planReimb: this.model.planReimb,
          }).then((res) => {
            if (res.success) {
              this.$message.warning((this.model.planReimb == 'zc' ? '支出' : '报销') + '的表单编码重复')
              that.confirmLoading = false
            } else {
              let formData = Object.assign(this.model, values)
              formData.isCopy = this.isCopy ? "1" : "0"
              httpAction(httpurl, formData, method)
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                  that.close()
                })
            }
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped>
</style>